﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using Telerik.Web.UI;

public partial class Admin_UserControls_ProfileManagement : System.Web.UI.UserControl
{
    private static int ProfileID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
    }
    protected void RadGrid1_iTemcommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Xem")
        {
            GridDataItem item = (GridDataItem)e.Item;
            ProfileID = int.Parse(item["profile_id"].Text);
            Panel1.Visible = false;
            Panel2.Visible = true;
            LoadData(ProfileID);
            return;
        }
        
        if (e.CommandName == "Sua")
        {
            GridDataItem item = (GridDataItem)e.Item;
            ProfileID = int.Parse(item["profile_id"].Text);
            Panel1.Visible = false;
            Panel2.Visible = true;
            LoadData(ProfileID);
            return;
        }

        if (e.CommandName == "Xoa")
        {
            GridDataItem item = (GridDataItem)e.Item;
            KetBanBonPhuong.Profile.Delete(int.Parse(item["profile_id"].Text));
            return;
        }

    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        DataTable dt = KetBanBonPhuong.Profile.GetAll();
        dt.Columns.Add("username");
        dt.Columns.Add("Gioitinh");
        foreach (DataRow dtRow in dt.Rows)
        {
            if ((bool)dtRow["gender"])
            {
                dtRow["Gioitinh"] = "Nam";
            }
            else
            {
                dtRow["Gioitinh"] = "Nữ";
            }
            User user = User.GetInfo((int)dtRow["user_id"]);
            dtRow["username"] = user.UserName;
        }
        RadGrid1.DataSource = dt;
        RadGrid1.DataBind();
    }
    protected void Button_OK_Click(object sender, EventArgs e)
    {
        KetBanBonPhuong.Profile profile = KetBanBonPhuong.Profile.GetInfo(ProfileID);
        profile.Nick_name = TextBox_Nickname.Text.Trim();
        profile.Dob = (DateTime)RadDatePicker_Birthday.SelectedDate;
        profile.Gender = (int.Parse(RadioButtonList_Gender.SelectedValue) == 0);
        profile.About_me = TextBox_About.Text;
        profile.Relationship = TextBox_Relationship.Text;
        profile.Looking_for = TextBox_Lockingfor.Text;
        profile.Phone = TextBox_Phone.Text;
        profile.Interests = TextBox_Interes.Text;
        profile.Education = DropDownList2.SelectedItem.Text;
        profile.Hobbies = TextBox_Hobbi.Text;
        try
        {
            KetBanBonPhuong.Profile.Update(profile);
            Panel2.Visible = false;
            Panel1.Visible = true;
        }
        catch (System.Exception Ex)
        {
            Label_Result.Text = Ex.ToString();
        }
        
    }

    protected void LoadData(int ProfileId)
    {
        KetBanBonPhuong.Profile profile = KetBanBonPhuong.Profile.GetInfo(ProfileId);
        User user = User.GetInfo(profile.User_id);
        Label_UserName.Text = user.UserName;
        Label_Rating.Text = profile.Rating.ToString();
        TextBox_Nickname.Text = profile.Nick_name;
        if (profile.Dob != null)
        {
            RadDatePicker_Birthday.SelectedDate = (DateTime)profile.Dob;
        }
        if (profile.Gender)
        {
            RadioButtonList_Gender.SelectedIndex = 0;
        }
        else
        {
            RadioButtonList_Gender.SelectedIndex = 1;
        }
        TextBox_About.Text = profile.About_me;
        TextBox_Relationship.Text = profile.Relationship;
        TextBox_Lockingfor.Text = profile.Looking_for;
        TextBox_Phone.Text = profile.Phone;
        TextBox_Interes.Text = profile.Interests;
        DropDownList2.SelectedItem.Text = profile.Education;
        TextBox_Hobbi.Text = profile.Hobbies;
    }
}
